import QtQuick 2.5
import QtQuick.Controls 2.1
import ".."

Popup
{
    id: myNumberKeyboard
    height: 600
    width: 1024
    property var callback: null
    onVisibleChanged: showText.text = ""
    background: Rectangle
    {
        color: Qt.rgba(0.8, 0.8, 0.8, 0.6)
    }
    function softkeyPressed(index, value)
    {
        var curLength = showText.text.length
        if(curLength >= 18) return
        if(showText.text == "0" && index !== 11)
        {
            if(index !== 9) showText.text = value
            return
        }
        if(curLength == 0 && index === 11)
        {
            showText.text = "0.";
            return
        }
        if(index === 9 && curLength != 0 && showText.text != "0")
        {
            if(showText.text[0] === '-') showText.text = showText.text.substr(1)
            else showText.text = "-" + showText.text
            return
        }
        if(index === 11 && showText.text.indexOf(".") != -1) return
        if(index === 9 && curLength == 0) return
        showText.text += value
    }

    Rectangle
    {
        id: numberWidget
        height: 420
        width: 350
        anchors.centerIn: parent
        radius: 4

        Text
        {
            id: numberTitle
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.margins: 20
            width: parent.width
            font.pixelSize: 16
            font.family: Common.fontMain
            color: Common.colorTextBlack
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
        }

        Rectangle
        {
            id: inputText
            height: 50
            anchors.top: numberTitle.bottom
            anchors.left: parent.left
            anchors.right: backButton.left
            anchors.margins: 20
            color: "#E0E0E0"
            radius: 4
            Text
            {
                id: showText
                anchors.fill: parent
                anchors.margins: 20
                font.pixelSize: 18
                font.family: Common.fontMain
                color: Common.colorTextBlack
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
            }
        }

        Image
        {
            id: backButton
            anchors.right: parent.right
            anchors.rightMargin: 20
            anchors.top: inputText.top
            sourceSize.height: 50
            source: "qrc:/Images/backspace.png"
            scale: backspaceArea.pressed ? 0.9 : 1
            antialiasing: true
            MouseArea
            {
                id: backspaceArea
                anchors.fill: parent
                onClicked:
                {
                    GET_UAC.playBeep()
                    showText.text = showText.text.substring(0, showText.text.length - 1)
                }
            }
        }

        Grid
        {
            id: numberGrid
            columns: 3
            rows: 4
            columnSpacing: 2
            rowSpacing: 2
            anchors.top: inputText.bottom
            anchors.topMargin: 15
            anchors.horizontalCenter: parent.horizontalCenter
            Repeater
            {
                model: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "+/-", "0", "."]
                delegate: Button
                {
                    text: modelData
                    width: (numberWidget.width - 4 - 40) / 3
                    height: width / 2
                    onClicked:
                    {
                        GET_UAC.playBeep()
                        myNumberKeyboard.softkeyPressed(index, modelData)
                    }
                }
            }
        }

        MyButton
        {
            id: acceptButton
            text: "确定"
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.horizontalCenterOffset: -80
            anchors.top: numberGrid.bottom
            anchors.topMargin: 20
            enabled: showText.text != ""
            onClicked:
            {
                myNumberKeyboard.callback(showText.text)
                myNumberKeyboard.close()
            }
        }

        MyButton
        {
            id: cancelButton
            text: "取消"
            isMain: false
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.horizontalCenterOffset: 80
            anchors.top: acceptButton.top
            onClicked:
            {
                myNumberKeyboard.callback(null)
                myNumberKeyboard.close()
            }
        }
    }
}
